<?php
class Training_ValidateController extends Zendvn_Controller_Action{
	
	protected $_arrParam;
	
	public function init(){
		$this->_arrParam = $this->_request->getParams();
	}
	
	public function index14Action(){
		$input = 'Khanh Pham';
		//$input = "84-88-38.373737";
		
		$validator = new Zendvn_Validate_PhoneNumber();
		
		if(!$validator->isValid($input)){
			echo '<br>' . 'Co loi xay ra';
			$message = $validator->getMessages();
			echo '<br>' . current($message);
		}
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function index13Action(){
		$password = 'Khanh Pham';
		$confirmPassword = 'khanh pham';
		
		$validator = new Zendvn_Validate_ConfirmPass($confirmPassword);
		
		if(!$validator->isValid($password)){
			echo '<br>' . 'Co loi xay ra';
			$message = $validator->getMessages();
			echo '<br>' . current($message);
		}
		$this->_helper->viewRenderer->setNoRender();
	}
	
	
	
	public function index12Action(){
		$input = 'Khanh Pham';
		
		$validator = new Zend_Validate();
		$options = array('table'=>'users',
						 'field'=>'user_name',						 
						);
		$validator->addValidator(new Zend_Validate_NotEmpty(), true)
				  ->addValidator(new Zend_Validate_Alnum(true),true)
				  ->addValidator(new Zend_Validate_StringLength(3,32),true)
				  ->addValidator(new Zend_Validate_Db_NoRecordExists($options),true)
				  ;
		
		if(!$validator->isValid($input)){
			$message = $validator->getMessages();
			echo '<br>' . current($message);
		}else{
			//luu vao database
		}
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function index11Action(){
		
		$input = "84-088-38.373737"; //XX-XX-XX.XXXXXX
		$pattern = '#[\d]{2}-[\d]{2}-[\d]{2}\.[\d]{6}#';
		$validator = new Zend_Validate_Regex($pattern);
		$validator->setMessage("'%value%' khong phai dinh dang so phone",Zend_Validate_Regex::NOT_MATCH);	
		
		if(!$validator->isValid($input)){
			$message = $validator->getMessages();
			echo '<br>' . current($message);
		}
		
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function index10Action(){
		
		//0. Gia tri can kiem tra;
		$input = "KhanhPham";
		
		//1. Khoi tao doi tuong Validate VD: Zend_Validate_StringLength
		$options = array('table'=>'users',
						 'field'=>'user_name',						 
						);
		$validator = new Zend_Validate_Db_NoRecordExists($options);	
				
		//3. Kiem tra du lieu dua vao VD: isValid($input)
		if(!$validator->isValid($input)){
			$message = $validator->getMessages();
			echo '<br>' . current($message);
		}
		
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function index9Action(){
		
		//0. Gia tri can kiem tra;
		$input = "vukhanh2212@gmail.com";
		
		//1. Khoi tao doi tuong Validate VD: Zend_Validate_StringLength
		$options = array('table'=>'users',
						 'field'=>'email',
						 'exclude'=>array('field'=>'id','value'=>1),
						);
		$validator = new Zend_Validate_Db_RecordExists($options);	
				
		//3. Kiem tra du lieu dua vao VD: isValid($input)
		if(!$validator->isValid($input)){
			$message = $validator->getMessages();
			echo '<br>' . current($message);
		}
		
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function index8Action(){
		
		//0. Gia tri can kiem tra;
		$input = "13-12-2011";
		$input = "13/12/2011";
		$input = "2011-12-23";
		
		//1. Khoi tao doi tuong Validate VD: Zend_Validate_StringLength
		//$validator = new Zend_Validate_Date(array('format'=>"dd-MM-YYYY"));	
		$validator = new Zend_Validate_Date();	
		$validator->setFormat("dd-MM-YYYY"); //HH:mm:
		
		
		//3. Kiem tra du lieu dua vao VD: isValid($input)
		if(!$validator->isValid($input)){
			$message = $validator->getMessages();
			echo '<br>' . current($message);
		}
		
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function index7Action(){
		
		//0. Gia tri can kiem tra;
		$input = 6;
		
		//1. Khoi tao doi tuong Validate VD: Zend_Validate_StringLength
		$validator = new Zend_Validate_Between(array('min'=>10,'max'=>100));		
		//$validator = new Zend_Validate_LessThan(1);		
		//$validator = new Zend_Validate_GreaterThan(10);		
		
		//3. Kiem tra du lieu dua vao VD: isValid($input)
		if(!$validator->isValid($input)){
			$message = $validator->getMessages();
			echo '<br>' . current($message);
		}
		
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function index6Action(){
		
		//0. Gia tri can kiem tra;
		$input = 'asdasd@yahoo.com.vn';
		
		//1. Khoi tao doi tuong Validate VD: Zend_Validate_StringLength
		$validator = new Zend_Validate_EmailAddress();		
		
		//3. Kiem tra du lieu dua vao VD: isValid($input)
		if(!$validator->isValid($input)){
			$message = $validator->getMessages();
			echo '<br>' . current($message);
		}
		
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function index5Action(){
		
		//0. Gia tri can kiem tra;
		$input = '';
		
		//1. Khoi tao doi tuong Validate VD: Zend_Validate_StringLength
		$validator = new Zend_Validate_NotEmpty();		
		
		//3. Kiem tra du lieu dua vao VD: isValid($input)
		if(!$validator->isValid($input)){
			$message = $validator->getMessages();
			echo '<br>' . current($message);
		}
		
		$this->_helper->viewRenderer->setNoRender();
	}
	
	
	public function index4Action(){
		//1. Khoi tao doi tuong Validate VD: Zend_Validate_StringLength
		//2. Thay doi thong bao loi VD: setMessage() - setMessages()
		//3. Kiem tra du lieu dua vao VD: isValid($input)
	}
	
	public function index3Action(){
		
		echo '<br>' . $input = 'sadsa';
		echo '<br>' . $input = 'sadsa sddasdb asjdjasd kjasjdk';
		$validator = new Zend_Validate_StringLength(10,20);
		/*$validator->setMessage("Chuoi '%value%' ngan '%min%' ky tu",Zend_Validate_StringLength::TOO_SHORT);
		$validator->setMessage("Chuoi '%value%' dai hon '%max%' ky tu",Zend_Validate_StringLength::TOO_LONG);*/
		
		$validator->setMessages(array(
								Zend_Validate_StringLength::TOO_SHORT =>"Chuoi '%value%' ngan '%min%' ky tu",
								Zend_Validate_StringLength::TOO_LONG =>"Chuoi '%value%' dai hon '%max%' ky tu",
								));
		
		if(!$validator->isValid($input)){			
			$message = $validator->getMessages();
			echo '<br>' . current($message);
			echo '<pre>';
			print_r($message);
			echo '</pre>';
		}
		
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function index2Action(){
		
		echo '<br>' . $input = 'sadsa';
		echo '<br>' . $input = 'sadsa sddasdb asjdjasd kjasjdk';
		$validator = new Zend_Validate_StringLength(10,20);
		if(!$validator->isValid($input)){			
			$message = $validator->getMessages();
			echo '<br>' . current($message);
			echo '<pre>';
			print_r($message);
			echo '</pre>';
		}
		
		$this->_helper->viewRenderer->setNoRender();
	}
	
	public function indexAction(){
		
		/*$input = 'sadsa sajbdhsad asjdsa';
		$input = 'sadsa';
		$validator = new Zend_Validate_StringLength(10);
		if($validator->isValid($input)){
			echo '<br>' . 'Thoa dieu khien';
		}else{
			echo '<br>' . 'Khong khoa dieu khien';
		}*/
		
		echo '<br>' . $input = 'sadsa sajbdhsad asjdsa';		
		echo '<br>' . $input = 'Đây là dàn bài chi tiết của khóa';		
		echo '<br>' . $input = 'Đây là dà';		
		//$validator = new Zend_Validate_StringLength(10,20);				
		$validator = new Zend_Validate_StringLength(array('min'=>10,'max'=>20,'encoding'=>'uft-8'));				
		if($validator->isValid($input)){
			echo '<br>' . 'Thoa dieu khien';
		}else{
			echo '<br>' . 'Khong khoa dieu khien';
		}
		
		$this->_helper->viewRenderer->setNoRender();
	}
	
}